﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FanPages.Common;
using FanPages.Server.Services.Contracts.Dto;
using FanPages.Server.Repostory.EFRepository.Model;

namespace FanPages.Server.Services.Transformers
{
    public class FixedLinkTransformerSvc : IServerTransformer<FixedLink,FixedLinkDto>
    {
        public FixedLinkDto ToDto(FixedLink entity)
        {
            var dto = new FixedLinkDto()
            {
                Description = entity.Description,
                Id = entity.Id,
                Link = entity.Link,
                Title = entity.Title,
                FanItem = new FanItemTransformerSvc().ToDto(entity.FanItem)
            };
            return dto;
        }

        public FixedLink ToClient(FixedLinkDto dto)
        {
            var entity = new FixedLink()
            {
                Description = dto.Description,
                Id = dto.Id,
                Link = dto.Link,
                Title = dto.Title,                
            };
            entity.FanItemReference.EntityKey = new System.Data.EntityKey("FanPagesEntities.FanItem", "Id", dto.FanItem.Id);
            return entity;
        }

        public List<FixedLinkDto> ToListDto(List<FixedLink> list)
        {
            var newList = new List<FixedLinkDto>();
            list.ForEach(x =>
            {
                newList.Add(ToDto(x));
            });
            return newList;
        }

        public List<FixedLink> ToListEntity(List<FixedLinkDto> list)
        {
            var newList = new List<FixedLink>();
            list.ForEach(x =>
            {
                newList.Add(ToClient(x));
            });
            return newList;
        }

        public FixedLink ToExistingClient(FixedLinkDto dto, FixedLink entity)
        {            
           entity.Description = dto.Description;
           entity.Id = dto.Id;
           entity.Link = dto.Link;
           entity.Title = dto.Title;
           entity.FanItemReference.EntityKey = new System.Data.EntityKey("FanPagesEntities.FanItem", "Id", dto.FanItem.Id);
           return entity;
        }
    }
}
